<html xmlns:tomboy="http://beatniksoftware.com/tomboy" xmlns:link="http://beatniksoftware.com/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size"><head><META http-equiv="Content-Type" content="text/html; charset=utf-8"><title>rails</title><style type="text/css">
        
	body {  }
	h1 { font-size: xx-large;
     	     font-weight: bold;
     	     border-bottom: 1px solid black; }
	div.note {
		   position: relative;
		   display: block;
		   padding: 5pt;
		   margin: 5pt; 
		   white-space: -moz-pre-wrap; /* Mozilla */
 	      	   white-space: -pre-wrap;     /* Opera 4 - 6 */
 	      	   white-space: -o-pre-wrap;   /* Opera 7 */
 	      	   white-space: pre-wrap;      /* CSS3 */
 	      	   word-wrap: break-word;      /* IE 5.5+ */ }
	</style></head><body><div class="note" id="rails"><a name="rails"></a><h1>rails</h1>
1. 建立表外键级联
	rails g scaffold Post name
	rails g scaffold comment Post:references
	要在model中添加attr_accessible Post_id，要不子表保存不了
2. dropdown级联

def cityByprovince      
   province_id  = params[:province_id ]          
   @cities = City.find(:all ,:conditions =&gt;["province_id  = ?", province_id ],:order =&gt; "orderby")      
    render :text =&gt; @bacities.to_json          
end   
	&lt;div class="field"&gt;
 &lt;%= f.label :province_id, "State/Province" %&gt;&lt;br /&gt;
 &lt;%= collection_select(:user, :province_id, Province.find_all_by_country_id(1), :id, :name , options={:prompt =&gt; "-- Select --"} ) %&gt;
 &lt;/div&gt;
 &lt;div class="field"&gt;
 &lt;%= f.label :city_id %&gt;&lt;br /&gt;
 
&lt;%#= f.select :city_id , options_for_select([ "-- Select --"])%&gt;
 &lt;%= collection_select(:user, :city_id, <a style="color:#3465A4" href="City.find_all_by_province_id(@user.province_id),">City.find_all_by_province_id(@user.province_id),</a> :id, :name , options={:prompt =&gt; "-- Select --"}) %&gt;
 
 &lt;/div&gt;

&lt;script type="text/javascript"&gt;// &lt;![CDATA[
  $(document).ready(function() {
 
    var user_city_id_pointer = $('#user_city_id');
 
    $('#user_province_id').change(function() {
      var provinceId = $('#user_province_id :selected').val();
      if(provinceId != "" &amp;&amp; provinceId != 0) {
        $.ajax({
          url: '/cities/showCitiesByProvince/' + provinceId + '.json',
          type: 'GET',
          dataType: "json",
          contentType: 'application/json; charset=utf-8',
          cache: true,
          success: function(data) {
            user_city_id_pointer.empty();
            $("&lt;option /&gt;", {text: "-- Select --"}).appendTo(user_city_id_pointer);
            $(data).each(function () {
              $("&lt;option /&gt;", {val: this.id, text: this.name}).appendTo(user_city_id_pointer);
            });
        },
        error: function(jqXHR, textStatus, errorThrown) {
        alert('Please, try later.');
       }
    });
    } else {
        user_city_id_pointer.empty();
        $("&lt;option /&gt;", {text: "-- Select --"}).appendTo(user_city_id_pointer);
    }
 });
});
// ]]&gt;&lt;/script&gt;

3. error message
In your model:

validates_presence_of :address1, :message =&gt; "Put some address please" 
In your view

&lt;% m.errors.each do |attr,msg|  %&gt;
 &lt;%=msg%&gt;
&lt;% end %&gt;
If you do instead

&lt;%=attr %&gt; &lt;%=msg %&gt;
you get this error message with the attribute name

address1 Put some address please
if you want to get the error message for one single attribute

&lt;%= @model.errors[:address1] %&gt;

4. 多表级联查询
 @contact = Contact.joins(:Unit).joins(:Department).select("contacts.*,units.uname,departments.dname").find(params[:id])
<strike> 在</strike>model<strike>中</strike>添加uname,dname字段attr_accessible :uname, :dname

5. 静态下拉列表
 HEADERSHIP = ["chairman", "vice-chairman", "Director", "Vice-Director"]
 &lt;%= f.collection_select :headership, Contact::HEADERSHIP, :to_s, :titleize, {:prompt =&gt; true} %&gt;

6.验证
  <a style="color:#3465A4" href="http://ihower.tw/rails3/activerecord-lifecycle.html">http://ihower.tw/rails3/activerecord-lifecycle.html</a> 
 <a style="color:#3465A4" href="http://www.haogongju.net/art/812275">http://www.haogongju.net/art/812275</a>

7. nest json use include
@departments.to_json(:include =&gt; {:Units =&gt; {
                                            :include =&gt; {
                                                :Contacts =&gt; {:only =&gt; [:cname, :headership, :room, :office, :telephone, :email]}
                                                }, 
                                                :only =&gt; :uname
                                                }
                               },
                              :only =&gt; :dname
                              ) 
取表中特定当字段用:only []

</div></body></html>